package com.changyue.activity.me.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.changyue.bleLib.BleRssiDevice;
import com.changyue.temp.R;
import com.changyue.util.EmptyUtil;

import java.util.List;

public class DeviceListAdapter extends BaseAdapter {
    private List<BleRssiDevice> mDevices;
    private LayoutInflater mInflater;

    public DeviceListAdapter(Context context, List<BleRssiDevice> devices) {
        mInflater = LayoutInflater.from(context);
        mDevices = devices;
    }

    public int getCount() {
        return mDevices.size();
    }

    public Object getItem(int position) {
        return mDevices.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewGroup vg;

        if (convertView != null) {
            vg = (ViewGroup) convertView;
        } else {
            vg = (ViewGroup) mInflater.inflate(R.layout.device_item, null);
        }

        BleRssiDevice device = mDevices.get(position);

        String name = device.getBleName();
        if (EmptyUtil.isEmpty(name)) {
            name = "未知";
        }
        ((TextView) vg.findViewById(R.id.name)).setText(name);

        String address = device.getBleAddress();
        ((TextView) vg.findViewById(R.id.uuid)).setText(address);

        if (device.getRssi() > -60) {
            ((ImageView) vg.findViewById(R.id.sign_img)).setImageResource(R.drawable.sign_4);
        } else if(device.getRssi() > -70) {
            ((ImageView) vg.findViewById(R.id.sign_img)).setImageResource(R.drawable.sign_3);
        } else if (device.getRssi() > -80) {
            ((ImageView) vg.findViewById(R.id.sign_img)).setImageResource(R.drawable.sign_2);
        } else  {
            ((ImageView) vg.findViewById(R.id.sign_img)).setImageResource(R.drawable.sign_1);
        }

        String rssi = String.format("%ddBm", device.getRssi());
        ((TextView) vg.findViewById(R.id.sign)).setText(rssi);

        return vg;
    }
}
